package com.leo.foodmaster.admin.stock.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.stock.entity.Warehouse;
import com.leo.foodmaster.admin.stock.service.IWarehouseService;
import com.leo.foodmaster.admin.system.entity.SysUser;
import com.leo.foodmaster.admin.system.service.IUserService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.enums.StatusEnum;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 仓库管理 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-08-15
 */
@RestController
@Api("仓库管理")
@AllArgsConstructor
@RequestMapping("/stock/warehouse")
public class WarehouseController {
    private final IWarehouseService warehouseService;
    private final IUserService userService;

    @ApiOperation("条件分页查询仓库列表")
    @GetMapping("/list")
    public Result<IPage<Warehouse>> getWarehousePageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "仓库名称") @RequestParam(required = false) String name,
            @ApiParam(value = "仓库状态") @RequestParam(required = false) String status) {
        IPage<Warehouse> pageList  = warehouseService.getWarehousePageList(new Page<>(pageNum, pageSize), name, status);
        return Result.success(pageList);
    }

    @ApiOperation("查询公司所有仓库")
    @GetMapping("/listAll")
    public Result<List<Warehouse>> listAllWarehouse() {
        List<Warehouse> list = warehouseService.list(Wrappers.lambdaQuery(Warehouse.class)
                .eq(Warehouse::getOrganId, WebUtils.getUser().getOrganId())
                .eq(Warehouse::getStatus, StatusEnum.ENABLED.getCode()));
        return Result.success(list);
    }

    @ApiOperation("查询公司所有用户")
    @GetMapping("/listAllUser")
    public Result<List<SysUser>> listAllUser() {
        List<SysUser> list = userService.list(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getOrganId, WebUtils.getUser().getOrganId())
                .eq(SysUser::getStatus, StatusEnum.ENABLED.getCode()));
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增仓库")
    public Result<Object> addWarehouse(@RequestBody Warehouse warehouse) {
        return warehouseService.addWarehouse(warehouse);
    }

    @PutMapping
    @ApiOperation("修改仓库")
    public Result<Object> updateWarehouse(@RequestBody Warehouse warehouse) {
        return warehouseService.updateWarehouse(warehouse);
    }

}

